//
//  FZVoiceRecordView.m
//  EnglishTalk
//
//  Created by wangwei on 16/2/24.
//  Copyright © 2016年 Feizhu Tech. All rights reserved.
//

#import "FZVoiceRecordView.h"
#import "FZAlertWindow.h"

@interface FZVoiceRecordView ()
{
    BOOL _isShowing;
    int _currentDBLevel;
}

@end

@implementation FZVoiceRecordView

- (instancetype)init
{
    return [self initWithFrame:CGRectZero];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    CGRect rect = [UIApplication sharedApplication].keyWindow.frame;
    self = [super initWithFrame:CGRectMake(0, 0, rect.size.width , rect.size.height - 44)];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, rect.size.width , rect.size.height - 44)];
        bgView.backgroundColor = [UIColor colorWithWhite:.0 alpha:.5f];
        [self addSubview:bgView];
        
        self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, 145.f, 145.f)];
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self.contentView setMaskLayerWithCornerRadius:8.f];
        self.contentView.center = self.center;
        [self addSubview:self.contentView];
        
        self.durationLabel = [[UILabel alloc] initWithFrame:CGRectMake(16.f, 16.f, 0.f, 0.f)];
        self.durationLabel.font = [UIFont systemFontOfSize:15.f];
        self.durationLabel.width = self.contentView.width - self.durationLabel.left * 2.f;
        self.durationLabel.height = ceilf(self.durationLabel.font.lineHeight);
        self.durationLabel.backgroundColor = [UIColor clearColor];
        self.durationLabel.textColor = [UIColor whiteColor];
        [self.contentView addSubview:self.durationLabel];
        
        CGRect imageFrame;
        imageFrame.size = CGSizeMake(56, 56);
        imageFrame.origin.x = floorf((self.contentView.size.width - imageFrame.size.width) / 2.f);
        
        imageFrame.origin.y = 33.f;
        
        self.audioMeterBackgroundView = [[UIImageView alloc] initWithFrame:imageFrame];
        self.audioMeterBackgroundView.image = [UIImage imageNamed:@"sms_voice_record_0"];
        [self.contentView addSubview:self.audioMeterBackgroundView];
        
        self.audioMeterView = [[UIImageView alloc] initWithFrame:imageFrame];
        self.audioMeterView.size = CGSizeMake(30.f, 30.f);
        self.audioMeterView.backgroundColor = [FZStyleSheet defaultStyleSheet].color_1;
        self.audioMeterView.alpha = .5f;
        //[self.contentView addSubview:self.audioMeterView];
        
        self.warningView = [[UIImageView alloc] initWithFrame:imageFrame];
        self.warningView.contentMode = UIViewContentModeCenter;
        self.warningView.image = [UIImage imageNamed:@"sms_voice_alert"];;
        [self.contentView addSubview:self.warningView];
        
        self.tipsLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.tipsLabel.font = [UIFont systemFontOfSize:14.0];
        self.tipsLabel.left = 10.f;
        self.tipsLabel.width = self.contentView.width - self.tipsLabel.left * 2.f;
        self.tipsLabel.height = ceilf(self.tipsLabel.font.lineHeight);
        //self.tipsLabel.top = self.contentView.height - 20.f - self.tipsLabel.height;
        self.tipsLabel.bottom = self.contentView.height - 15.f;
        self.tipsLabel.backgroundColor = [UIColor clearColor];
        self.tipsLabel.textColor = [FZStyleSheet defaultStyleSheet].color_3;
        self.tipsLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:self.tipsLabel];
        
        self.state = -1;
        self.state = LCChatRecorderMetersAlertViewStateRecording;
        self.audioMeterProgress = 0.1;
        self.audioMeterProgress = 0.5;
        self.duration = -1;
        self.duration = 0;
    }
    return self;
}


- (void)setState:(LCChatRecorderMetersAlertViewState)state
{
    if (_state == state) {
        return;
    }
    
    _state = state;
    switch (state) {
        case LCChatRecorderMetersAlertViewStateRecording: {
            self.durationLabel.hidden = NO;
            self.audioMeterBackgroundView.hidden = NO;
            self.audioMeterView.hidden = NO;
            self.warningView.hidden = YES;
            self.tipsLabel.text = @"手指上滑  取消发送";
        } break;
        case LCChatRecorderMetersAlertViewStateCancelTips: {
            self.durationLabel.hidden = YES;
            self.audioMeterBackgroundView.hidden = YES;
            self.audioMeterView.hidden = YES;
            self.warningView.hidden = NO;
            self.warningView.image = [UIImage imageNamed:@"sms_voice_delete"];
            self.tipsLabel.text = @"松开手指  取消发送";
        } break;
        case LCChatRecorderMetersAlertViewStateWarning: {
            self.durationLabel.hidden = YES;
            self.audioMeterBackgroundView.hidden = YES;
            self.audioMeterView.hidden = YES;
            self.warningView.hidden = NO;
            self.warningView.image = [UIImage imageNamed:@"sms_voice_alert"];
            self.tipsLabel.text = @"说话时间太短";
        } break;
        default:
            break;
    }
}

- (void)setAudioMeterProgress:(float)progress
{
    int imageCount = 0.f;
    
    if(progress == 0.f) {
        imageCount = 0;
    } else if (progress > 0 && progress <= 15) {
        imageCount = 1;
    } else if (progress > 15 && progress <= 30) {
        imageCount = 2;
    } else if (progress > 30 && progress <= 45) {
        imageCount = 3;
    } else if (progress > 45 && progress <= 60) {
        imageCount = 4;
    } else if (progress > 60 && progress <= 75) {
        imageCount = 5;
    } else if (progress > 75 && progress <= 90) {
        imageCount = 6;
    } else if (progress > 90 && progress <= 105) {
        imageCount = 7;
    } else if (progress > 105 && progress <= 120) {
        imageCount = 8;
    } else if (progress > 120 && progress <= 135) {
        imageCount = 9;
    }
    NSLog(@"@@@@@@@@@@@@@%d",imageCount);
    if (_audioMeterProgress != progress) {
        _audioMeterProgress = progress;
        self.audioMeterBackgroundView.image = [UIImage imageNamed:[NSString stringWithFormat:@"sms_voice_record_%d",imageCount]];
    }
}

- (void)setDuration:(int)duration
{
    if (duration != _duration) {
        _duration = duration;
        if (duration >= 50) {
            if(60 - duration > 0 && duration < 59.f) {
                self.tipsLabel.text = NSStringWith(@"%ds", 60 - duration);
            } else if (duration >= 59.f) {
                self.tipsLabel.text = @"说话时间超长";
            }
        }
        //self.durationLabel.text = NSStringWith(@"%d˝", duration);
    }
}


- (void)show
{
    if (!_isShowing)
    {
        self.durationLabel.hidden = NO;
        self.audioMeterBackgroundView.hidden = NO;
        self.audioMeterView.hidden = NO;
        self.warningView.hidden = YES;
        self.tipsLabel.text = @"手指上滑  取消发送";
        _isShowing = YES;
        [[FZAlertWindow sharedWindow] showView:self];
        [FZAlertWindow sharedWindow].height = self.height;
    }
}

- (void)dismiss
{
    if (_isShowing)
    {
        _isShowing = NO;
        [[FZAlertWindow sharedWindow] dismissView:self];
        [FZAlertWindow sharedWindow].height = [UIScreen mainScreen].bounds.size.height;
    }
}

- (BOOL)isShowing
{
    return _isShowing;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
